package logger

import (
	"os"

	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
)

var (
	level = zapcore.InfoLevel
	Log   *zap.SugaredLogger
)

func InitAndSetLogLevel(strLevel string) *zap.SugaredLogger {

	switch strLevel {
	case "debug":
		level = zapcore.DebugLevel
	case "info":
		level = zapcore.InfoLevel
	case "warn":
		level = zapcore.WarnLevel
	case "error":
		level = zapcore.ErrorLevel
	default:
		level = zapcore.DebugLevel
	}
	atom := zap.NewAtomicLevelAt(level)
	encoderCfg := zap.NewDevelopmentEncoderConfig()

	Log = zap.New(zapcore.NewCore(
		zapcore.NewConsoleEncoder(encoderCfg),
		zapcore.Lock(os.Stdout),
		atom,
	)).Sugar()

	Log.Infof("set log level %v", level)
	return Log
}
